package itil

import (
	db "fjj/tool/dbOperate"
	uDing "fjj/tool/ding"
	"github.com/gin-gonic/gin"
	"net/http"
)

func test4(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"test4": "员工表",
	})
}


//把钉钉在职人员ID列表写入数据库
func WriteDingEmployeeIdToDb() error {
	conn := db.DbConn()
	defer conn.Close()

	//自动迁移,数据库结构可适应结构而自动增加列，旧有列不会减
	conn.AutoMigrate(&db.EmployeeInfo{})

	ack:=uDing.GetAccessToken()
	IdList:=uDing.GetAllEmployeeIdList(&ack)

	//开启事务
	tx:=conn.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if tx.Error != nil {
		return tx.Error
	}

	//在事务中创建记录
	for key,value:=range IdList{
		u := db.EmployeeInfo{DingID: value}

		if err := tx.Create(&u).Error; err != nil {
			tx.Rollback()
			return err
		}
		println(key)
	}

	//提交事务,写入数据库
	return tx.Commit().Error
}